package controllers;

import java.awt.event.ActionEvent;
import java.util.Vector;
import dialogs.DeleteTupleDialog;
import dialogs.InsertTupleDialog;
import models.HasSongModel;
import system.View;

public class HasSongController extends AbstractMenuItemController {
	private HasSongModel has_song = new HasSongModel();

	public HasSongController(View v) {
		super(v);
		abstractModel = has_song;
		has_song.addExceptionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();

		if (actionCommand.equals("Dissasociate Song"))
			showDialogWindow(new DeleteTupleDialog(view, actionCommand, "Key",
					new String[] { "Item UPC: ", "Song Name: " }, this));
		else if (actionCommand.equals("Set Song"))
			showDialogWindow(new InsertTupleDialog(view, actionCommand, "Song",
					new String[] { "Item UPC: ", "Song Name: " }, this));
	}

	@Override
	public int validateInsert(Vector<String> s) {
		if (s.size() != 2)
			return VALIDATIONERROR;

		try {
			Integer upc;
			String title;

			if (s.get(0).trim().length() != 0)
				upc = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;
			if (s.get(1).trim().length() != 0)
				title = s.get(1).trim();
			else return VALIDATIONERROR;

			view.updateStatusBar("Setting Song...");

			if (has_song.insert(upc, title)) return operationSuccess();
			else  return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}

	@Override
	public int validateDelete(Vector<String> s) {
		if (s.size() != 2)
			return VALIDATIONERROR;

		try {
			Integer upc;
			String title;

			if (s.get(0).trim().length() != 0)
				upc = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;
			if (s.get(1).trim().length() != 0)
				title = s.get(1).trim();
			else return VALIDATIONERROR;

			view.updateStatusBar("Disassociating Song...");

			if (has_song.delete(upc, title)) return operationSuccess();
			else return operationFailure();
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
	}
}
